package com.mywang.weitravel.modules.mine;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.mywang.weitravel.R;
import com.mywang.weitravel.base.BaseActivity;
import com.mywang.weitravel.model.CollectionResponse;
import com.mywang.weitravel.modules.find.FindDetailActivity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

public class CollectionActivity extends BaseActivity<CollectionPresenter> implements CollectionContract.View, OnRefreshLoadmoreListener {

    public static void launch(Activity activity) {
        Intent intent = new Intent(activity, CollectionActivity.class);
        launchActivity(activity, intent);
    }

    @BindView(R.id.iv_title_back)
    ImageView ivTitleBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.srl_layout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.rv_collection)
    RecyclerView rvCollection;
    private CollectionAdapter collectionAdapter;

    @Override
    protected void initView() {
        tvTitle.setText("我的收藏");
        rvCollection.setLayoutManager(new LinearLayoutManager(this));
        collectionAdapter = new CollectionAdapter();
        rvCollection.setAdapter(collectionAdapter);
        collectionAdapter.setOnItemOnclickListener(new CollectionAdapter.OnItemOnclickListener() {
            @Override
            public void onItemClick(CollectionResponse.CollectItem item, int position) {
                FindDetailActivity.launch(CollectionActivity.this, item.c_id);
            }
        });
        smartRefreshLayout.setOnRefreshLoadmoreListener(this);
        smartRefreshLayout.setEnableLoadmore(false);
        smartRefreshLayout.autoRefresh();
    }

    @OnClick({R.id.iv_title_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_title_back:
                finish();
                break;
        }
    }

    @Override
    protected CollectionPresenter initPresenter() {
        return new CollectionPresenter(this);
    }

    @Override
    protected int getActivityLayoutID() {
        return R.layout.activity_collection;
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        mPresenter.onRefresh();
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        mPresenter.onLoadMore();
    }


    @Override
    public void onRefreshSuccessful(List<CollectionResponse.CollectItem> collections, boolean hasMore) {
        smartRefreshLayout.setEnableRefresh(true);
        smartRefreshLayout.setEnableLoadmore(hasMore);
        collectionAdapter.setData(collections);
    }

    @Override
    public void onLoadMoreSuccessful(List<CollectionResponse.CollectItem> collections, boolean hasMore) {
        smartRefreshLayout.setEnableLoadmore(hasMore);
        collectionAdapter.addData(collections);
    }

    @Override
    public void refreshOrLoadMoreCompleted() {
        smartRefreshLayout.finishRefresh();
        smartRefreshLayout.finishLoadmore();
    }

    @Override
    public void noData() {
        smartRefreshLayout.setEnableRefresh(false);
        smartRefreshLayout.setEnableLoadmore(false);
    }
}
